# _*_ encoding:utf-8 _*_

from django.db.models import Q
from rest_framework.viewsets import ModelViewSet
from rest_framework.generics import ListAPIView
from rest_framework.permissions import IsAdminUser

from goods.models import SKU, GoodsCategory, SPU, SPUSpecification
from meiduo_admin.serializers.sku_serializers import SKUModelSerializer, \
    GoodsCategryModelSerializer, SPUSimpleModelSerializer, SPUSpecModelSerializer

from meiduo_admin.paginations import MyPage
from meiduo_mall.utils.django_drf_filter import get_filter


class SpecOptView(ListAPIView):
    permission_classes = [IsAdminUser]

    queryset = SPUSpecification.objects.all()
    serializer_class = SPUSpecModelSerializer

    def get_queryset(self):
        spu_id = self.kwargs.get('pk')

        return self.queryset.filter(spu_id=spu_id)


class SPUSimpleView(ListAPIView):
    permission_classes = [IsAdminUser]

    queryset = SPU.objects.all()
    serializer_class = SPUSimpleModelSerializer


class SKUGoodsCateView(ListAPIView):
    permission_classes = [IsAdminUser]

    queryset = GoodsCategory.objects.all()
    serializer_class = GoodsCategryModelSerializer

    def get_queryset(self):
        if self.request.path_info.split('/')[2] == 'skus':
            """SKU管理-新增商品-获取三级分类接口"""

            return self.queryset.filter(parent_id__gt=37)

        elif self.request.path_info.split('/')[2] == 'goods':
            if self.kwargs.get('pk'):
                """SPU管理-新增商品-获取一级分类数据接口"""

                return self.queryset.filter(parent_id=self.kwargs.get('pk'))
            else:
                """SPU管理-新增商品-获取二、三级分类数据接口"""
                return self.queryset.filter(parent_id=None)

        # 涉及到读取、判定数据效率慢
        # categrys = []
        # for one_categrys in self.queryset.filter(parent=None):
        #     for two_categrys in self.queryset.filter(parent=one_categrys):
        #         three_categrys = self.queryset.filter(parent=two_categrys)
        #         for categry in three_categrys:
        #             categrys.append(categry)
        #
        # return categrys

        # 涉及到读取、判定数据效率慢
        # def get_three_categry(categry):
        #
        #     try:
        #         if categry.parent.parent.parent is None:
        #             return categry
        #     except Exception as e:
        #         return None
        #
        # categrys = set(get_three_categry(categry) for categry in self.queryset.all())
        # categrys.remove(None)

        # return list(categrys)


class SKUGoodsView(ModelViewSet):
    permission_classes = [IsAdminUser]

    queryset = SKU.objects.all()
    serializer_class = SKUModelSerializer
    pagination_class = MyPage

    filter_class = get_filter(SKU, 'name', 'caption')  # 指定过滤器

    # def get_queryset(self):
    #
    #     keyword = self.request.query_params.get('keyword')
    #
    #     if keyword:
    #         return self.queryset.filter(Q(name__contains=keyword) | Q(caption__contains=keyword))
    #     else:
    #         return self.queryset.all()

